package cn.demarcia.coco.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.demarcia.coco.R;
import cn.demarcia.coco.uihelper.StringUtils;
import cn.demarcia.coco.uihelper.UIHelper;
import info.jimao.newsdk.models.NewsInfoModel;

/**
 * Created by 智尧
 * on 2016/7/19.
 */
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
    private Context mContext;
    private List<Object> mItems = new ArrayList<>();
    private final LayoutInflater mLayoutInflater;

    public NewsAdapter(Context context, List<Object> items) {
        this.mContext = context;
        this.mItems = items;
        this.mLayoutInflater = LayoutInflater.from(context);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.list_item_news, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final NewsInfoModel item = (NewsInfoModel) mItems.get(position);
        holder.tvDate.setText(item.date);
        holder.tvAuthor.setText(item.author_name);
        holder.tvTitle.setText(item.title);
        if (!StringUtils.isEmpty(item.thumbnail_pic_s)) {
            holder.ivPic1.setImageURI(Uri.parse(item.thumbnail_pic_s));
        } else {
            holder.ivPic1.setImageResource(R.drawable.ic_sentiment_dissatisfied_white_48dp);
        }


        holder.llHolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIHelper.showWebViewActivity(mContext, item);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mItems == null ? 0 : mItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tvDate)
        TextView tvDate;
        @Bind(R.id.tvTitle)
        TextView tvTitle;
        @Bind(R.id.ivPic1)
        SimpleDraweeView ivPic1;
        @Bind(R.id.tvAuthor)
        TextView tvAuthor;
        @Bind(R.id.rlHolder)
        RelativeLayout llHolder;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}